<template>

  <div id="home">
    <!-- 导航栏 -->
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行', '新款', '精选']" @tabClick="tabClick" ref="tabControl1"
      class="tab-control" v-show="isTabFixed" />
    <!-- 滑动插件 -->
    <scroll class=" content" ref="scroll" :probe-type="3" @scroll="contentScroll"
      :pull-up-load="true" @pullingUp="loadMore">
      <!-- 轮播图 -->
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <!-- 推荐信息 -->
      <recommend-view :recommends="recommend" />
      <!-- 推荐信息2 -->
      <feature-view />
      <!-- 导航分类 -->
      <tab-control :titles="['流行', '新款', '精选']" @tabClick="tabClick" ref="tabControl2"
        v-show="!isTabFixed" />
      <!-- 商品列表 -->
      <goods-list :goods="showGoods" />
    </scroll>

    <!-- 回到顶部按钮 -->
    <back-top @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";

import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
// import BackTop from "components/content/backTop/BackTop";

import { getHomeMultidata, getHomeGoods } from "network/home";
import { debounce } from 'common/utils'
import { itemListenerMixin, backTopMixin } from 'common/mixin'

export default {
  name: "Home",
  components: {
    HomeSwiper,
    RecommendView,
    FeatureView,
    NavBar,
    TabControl,
    GoodsList,
    Scroll
  },
  mixins: [backTopMixin],
  data () {
    return {
      banners: [],
      recommend: [],
      // 商品数据
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] }
      },
      currentType: "pop",
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
      itemImgListener: null
    };
  },
  // 计算属性
  computed: {
    showGoods () {
      return this.goods[this.currentType].list;
    }
  },
  // 活跃状态（表示当前路由被显示）
  activated () {
    this.$refs.scroll.scrollTo(1, this.saveY, 1)
    this.$refs.scroll.refresh()
  },
  // 离开状态
  deactivated () {
    // 获取y的值
    this.saveY = this.$refs.scroll.getScrollY()

    // 取消全局事件的监听
    this.$bus.$off('itemImageLoad', this.itemImgListener)
  },
  // 生命周期函数
  created () {
    // 请求多个数据
    this.getHomeMultidata();

    // 请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  mounted () {
    // 监听item中图片加载完成
    const newrefresh = debounce(this.$refs.scroll.refresh, 500)

    // 对监听事件的保存
    this.itemImgListener = () => {
      newrefresh()
    }

    // 事件总线 监听事件
    this.$bus.$on('itemImageLoad', this.itemImgListener)

  },
  // 监听事件
  methods: {
    /**
     * 事件监听相关的方法
     */
    tabClick (index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    // 监听滚动位置
    contentScroll (position) {
      // 1.判断BackTop是否显示
      this.isShowBackTop = position.y < -900;
      // console.log(position.y);
      // 2. 决定tabControl是否吸顶
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
    // 加载更多商品
    loadMore () {
      // console.log("加载跟多");
      this.getHomeGoods(this.currentType);
    },
    // 监听轮播图 图片加载完成
    swiperImageLoad () {
      // 获取tabControl的offsetTop
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    /**
     * 网络请求相关的方法
     */
    getHomeMultidata () {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list;
        this.recommend = res.data.recommend.list;
      });
    },
    // 加载数据
    getHomeGoods (type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        // 刷新上拉加载事件
        this.$refs.scroll.finishPullUp();
      });
    }
  }
};
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}

.home-nav {
  /* css 使用变量的方法 */
  background-color: var(--color-tint);
  color: white;

  /* position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 2; */
}

.content {
  height: calc(100% - 44px);
  overflow: hidden;
}
.tab-control {
  position: relative;
  z-index: 5;
}
</style>
